<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/6/29
 * Time: 14:27
 * Created by Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Panx;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Panx\DeviceSpec\DeviceSpecStoreRequest;
use App\Http\Requests\Admin\Panx\DeviceSpec\DeviceSpecUpdateRequest;
use App\Models\Panx\DeviceSpec;
use App\Services\Utils\RequestParam;
use Illuminate\Http\Request;
use Tests\Feature\Admin\Customer\DeviceSpecControllerTest;

class DeviceSpecController extends ApiController
{
    const ROUTE_NAMES = [
        'index'   => 'device-spec.index',
        'store'   => 'device-spec.store',
        'update'  => 'device-spec.update',
        'destroy' => 'device-spec.destroy',
    ];

    /**
     *
     * @test
     * @see DeviceSpecControllerTest::test_panx_device_spec_index()
     *
     * @param Request $request
     * @return array|\Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $device_specs = (new DeviceSpec)
            ->latest()
            ->paginate($request->per_page ?: 15);

        return $this->response(compact('device_specs'));
    }

    /**
     * 返回device_specs表的字段名和默认参数值
     *
     * @return array|\Illuminate\Http\Response
     */
    public function create()
    {
        $device_spec = DeviceSpec::DEVICE_SPEC_KEYS;

        return $this->response(compact('device_spec'));
    }

    /**
     * @test
     *
     * @param DeviceSpecStoreRequest $request
     * @return array|\Illuminate\Http\Response
     */
    public function store(DeviceSpecStoreRequest $request)
    {
        $store_data = RequestParam::normalize(array_keys($request->rules()));

        $store =  (new DeviceSpec)->create($store_data);

        return $this->response(compact('store'));
    }

    /**
     * @test
     *
     * @param DeviceSpecUpdateRequest $request
     * @param $id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(DeviceSpecUpdateRequest $request, $id)
    {
        /** @var DeviceSpec $device_spec */
        $device_spec = (new DeviceSpec)->find($id);
            $this->checkItemExist($device_spec);

        $update_data = RequestParam::normalize(array_keys($request->rules()));

        $update = $device_spec->update($update_data);

        return $this->response(compact('update'));
    }

    /**
     * @test
     * @see DeviceSpecControllerTest::test_panx_device_spec_destroy()
     *
     * @param $id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($id)
    {
        /** @var DeviceSpec $device_spec */
        $device_spec = (new DeviceSpec)->find($id);
            $this->checkItemExist($device_spec);

        $count = $device_spec->devices()->count();
            $this->checkUnless($count,trans('admin.device_spec_used'));
            $this->checkUnless(
                $id === DeviceSpec::ID_DEFAULT,
                trans('admin.default_device_spec_not_delete'
            ));

        $delete =  $device_spec->delete();

        return $this->response(compact('delete'));

    }
}